package org.numanint.wizard;

import java.sql.ResultSet;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.numanint.utils.UIutils;

public class WebProjectWizardThree  extends WizardPage {
 
	protected WebProjectWizardThree(String pageName) {
		super(pageName);
	
		
	}

	@Override
	public void dispose() {   
		 super.dispose();
		 setControl(null);   
		}
	
	public WebProjectWizardThree() {
		super("Numanint-新建一个Web框架");
		setTitle("Model层数据选择设置");
		setDescription("选择Model层需要持久化的表对象,注意表字段请不要使用日期类型，尽量使用整形和字符串类型，已方便JAVA转换");
		setImageDescriptor(UIutils.getImageDescriptor("icons/numanint-48.png"));
		setPageComplete(false);

	}



	public void createControl(Composite parent) {
		initializeDialogUnits(parent);
		final Composite composite = createClientArea(parent);
		paint(composite);
		setControl(composite);
		Dialog.applyDialogFont(composite);
	}
	
	private Composite createClientArea(Composite composite) {
		Composite composite1 = new Composite(composite, SWT.NONE);
		GridLayout gridlayout = new GridLayout();		 
		gridlayout.marginWidth = 20;
		gridlayout.marginHeight = 20;
		gridlayout.numColumns = 2;
		composite1.setLayout(gridlayout);
		return composite1;
	}
	
	
	private void paint(Composite composite) {
		final Label labelPojoPkg = new Label(composite, 64);
		labelPojoPkg.setText("POJO包：");
		final Text textPojoPkg = new Text(composite,SWT.SINGLE);
		textPojoPkg.setText(WebProjectConfig.pojoPkgName);
		textPojoPkg.addModifyListener(new ModifyListener(){public void modifyText(ModifyEvent e) {WebProjectConfig.pojoPkgName = ((Text) e.getSource()).getText();}});		
		final Label labelDaoPkg = new Label(composite, 64);
		labelDaoPkg.setText("DAO包：");
		final Text textDaoPkg = new Text(composite,SWT.SINGLE);
		textDaoPkg.setText(WebProjectConfig.daoPkgName);
		textDaoPkg.addModifyListener(new ModifyListener(){public void modifyText(ModifyEvent e) {WebProjectConfig.daoPkgName = ((Text) e.getSource()).getText();}});  
		final Label labelBoPkg = new Label(composite, 64);
		labelBoPkg.setText("BO包：");
		final Text textBoPkg = new Text(composite,SWT.SINGLE);
		textBoPkg.setText(WebProjectConfig.boPkgName);
		textBoPkg.addModifyListener(new ModifyListener(){public void modifyText(ModifyEvent e) {WebProjectConfig.boPkgName = ((Text) e.getSource()).getText();}});
		final Label labelControllerPkg = new Label(composite, 64);
		labelControllerPkg.setText("Conetroller包：");
		final Text textControllerPkg = new Text(composite,SWT.SINGLE);
		textControllerPkg.setText(WebProjectConfig.controllerPkgName);
		textControllerPkg.addModifyListener(new ModifyListener(){public void modifyText(ModifyEvent e) {WebProjectConfig.controllerPkgName = ((Text) e.getSource()).getText();}});
		final Label labelValidatorPkg = new Label(composite, 64);
		labelValidatorPkg.setText("Validator包：");
		final Text textValidatorPkg = new Text(composite,SWT.SINGLE);
		textValidatorPkg.setText(WebProjectConfig.validatorPkgName);
		textValidatorPkg.addModifyListener(new ModifyListener(){public void modifyText(ModifyEvent e) {WebProjectConfig.validatorPkgName = ((Text) e.getSource()).getText();}});
		final Label labeltableList = new Label(composite, 64);
		labeltableList.setText("选择表：");
		final List tableList = new List (composite, SWT.BORDER|SWT.V_SCROLL|SWT.MULTI);	
		
		if(WebProjectConfig.conn!=null&&WebProjectConfig.selectedSchema!=null){
			try{
				ResultSet rs =  WebProjectConfig.conn.getMetaData().getTables(null, null, null, 
				         new String[] {"TABLE"});
				while(rs.next()){
					tableList.add(rs.getString("TABLE_NAME"));
				}
				rs.close();
			}catch(Exception e){
				e.printStackTrace();
			}

		}
		tableList.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				WebProjectConfig.projectModelTableNames = tableList.getSelection();
				setPageComplete(true);
			}
		});
		
		labelPojoPkg.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		textPojoPkg.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		labelDaoPkg.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		textDaoPkg.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		labelBoPkg.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		textBoPkg.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		labelControllerPkg.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		textControllerPkg.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		labelValidatorPkg.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		textValidatorPkg.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		labeltableList.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		tableList.setLayoutData(new GridData(GridData.FILL_BOTH));
	}

	
}
